package cn.apthink.utility;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class TreeUtil {
	/**
	 * 返回树型结构指定uid及其下属节点uid拼接的字符串
	 *
	 * @param pid
	 * @param data
	 * @return
	 */
	public static String getUidString(String pid, List<TreeNode> data) {
		StringBuilder s = new StringBuilder();
		s.append(pid);
		List<TreeNode> lst = data.stream().filter(x -> {
			return pid.equals(x.getPid());
		}).collect(Collectors.toList());
		lst.forEach(x -> {
			s.append(",").append(getUidString(x.getId(), data));
		});
		return s.toString();
	}

	/**
	 * 返回树型结构uid列表及其各自下属节点uid拼接的字符串
	 *
	 * @param uids
	 * @param data
	 * @return
	 */
	public static String getUidString(List<String> uids, List<TreeNode> data) {
		StringBuilder s = new StringBuilder();
		uids.forEach(x -> {
			s.append(",").append(getUidString(x, data));
		});
		return s.substring(1);
	}

	/**
	 * 递归返回�?有节�?
	 *
	 * @param pid
	 * @param data
	 * @param orgData
	 * @return
	 */
	private static void filterNode(String uid, Set<String> nodes, List<TreeNode> orgData) {
		if (!nodes.contains(uid))
			nodes.add(uid);
		List<TreeNode> ds = orgData.stream().filter(d -> {
			return uid.equals(d.getPid());
		}).collect(Collectors.toList());
		if (ds.size() > 0) {
			ds.forEach(d -> {
				filterNode(d.getId(), nodes, orgData);
			});
		}
	}

	/**
	 * 返回树型结构uid列表及其各自下属节点uid的List
	 *
	 * @param uids
	 * @param data
	 * @return
	 */
	public static Set<String> getChildUids(String uid, List<TreeNode> data) {
		HashSet<String> result = new HashSet<String>();
		filterNode(uid, result, data);
		return result;
	}

	public static Set<String> getChildUids(List<String> uids, List<TreeNode> data) {
		HashSet<String> result = new HashSet<String>();
		uids.forEach(x -> {
			filterNode(x, result, data);
		});
		return result;
	}

	public static Set<String> getTopUids(List<TreeNode> data) {
		HashSet<String> result = new HashSet<String>();
		data.forEach(x -> {
			if (Util.isEmpty(x.getPid())) {
				result.add(x.getId());
			}
		});
		return result;
	}

	/**
	 * 递归返回�?有父节点
	 *
	 * @param pid
	 * @param data
	 * @param orgData
	 * @return
	 */
	private static void filterParent(String uid, Set<String> nodes, List<TreeNode> orgData) {
		Optional<TreeNode> ds = orgData.stream().filter(d -> {
			return uid.equals(d.getId());
		}).findFirst();
		if (ds.isPresent()) {
			String p = ds.get().getPid();
			if (p != null && p.length() > 0) {
				if (!nodes.contains(p))
					nodes.add(p);
				filterParent(p, nodes, orgData);
			}
		}
	}

	/**
	 * 返回树型结构中各自所有父节点uid的Set
	 *
	 * @param uids
	 * @param data
	 * @return
	 */
	public static Set<String> getParentUidList(String uid, List<TreeNode> data) {
		HashSet<String> result = new HashSet<String>();
		if (!Util.isEmpty(uid) && !Util.isEmpty(data))
			filterParent(uid, result, data);
		return result;
	}

	public static Set<String> getParentUidList(List<String> uids, List<TreeNode> data) {
		HashSet<String> result = new HashSet<String>();
		uids.forEach(x -> {
			filterParent(x, result, data);
		});
		return result;
	}

	/**
	 * 返回树型结构中指定节点的父类顶端节点uid
	 *
	 * @param uids
	 * @param data
	 * @return
	 */
	public static String getTopUid(String uid, List<TreeNode> data) {
		TreeNode result = data.stream().filter(x->{
			return x.getId().equals(uid);
		}).findAny().get();
		while (!Util.isEmpty(result.getPid())) {
			String pid = result.getPid();
			result = data.stream().filter(x->{
				return x.getId().equals(pid);
			}).findAny().get();
		}
		return result.getId();
	}

	/**
	 * 返回树型结构uid列表及其各自下属和父类节点uid的List
	 *
	 * @param uids
	 * @param data
	 * @return
	 */
	public static Set<String> getAllUidList(List<String> uids, List<TreeNode> data) {
		HashSet<String> result = new HashSet<String>();
		uids.forEach(x -> {
			filterNode(x, result, data);
		});
		uids.forEach(x -> {
			filterParent(x, result, data);
		});
		return result;
	}

	/**
	 * 递归返回�?有叶子节�?
	 *
	 * @param pid
	 * @param data
	 * @param orgData
	 * @return
	 */
	private static void filterLeafNode(String uid, Set<String> leaves, List<TreeNode> orgData) {
		List<TreeNode> ds = orgData.stream().filter(d -> {
			return uid.equals(d.getPid());
		}).collect(Collectors.toList());
		if (ds.size() > 0) {
			ds.forEach(d -> {
				filterLeafNode(d.getId(), leaves, orgData);
			});
		} else {
			leaves.add(uid);
		}
	}

	/**
	 * 返回指定单位下所有叶子节�?
	 *
	 * @param pid
	 * @param data
	 * @param orgData
	 * @return
	 */
	public static Set<String> getLeafNodes(String uid, List<TreeNode> orgData) {
		HashSet<String> leaves = new HashSet<String>();
		filterLeafNode(uid, leaves, orgData);
		return leaves;
	}

	public static Set<String> getLeafNodes(List<String> uids, List<TreeNode> orgData) {
		Set<String> leaves = new HashSet<String>();
		uids.forEach(x -> {
			filterLeafNode(x, leaves, orgData);
		});
		return leaves;
	}

}
